package cz.techi;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.*;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class ScheduleDb {

   private static final String DATABASE_NAME = "subjects6.db";
   private static final int DATABASE_VERSION = 1;
   private static final String TABLE_NAME = "subjects";

   private Context context;
   private SQLiteDatabase db;

   private SQLiteStatement insertStmt;
   private static final String INSERT = "insert into " 
      + TABLE_NAME + "(ident, name, hour, day, time_from, time_to, teacher, room, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";

   public ScheduleDb(Context context) {
      this.context = context;
      OpenHelper openHelper = new OpenHelper(this.context);
      this.db = openHelper.getWritableDatabase();
      this.insertStmt = this.db.compileStatement(INSERT);
      
   }
   
   

   public long insert(Subject subject) {
	   
	   int i = 0;
	   
	  this.insertStmt.bindString(++i, subject.ident);
      this.insertStmt.bindString(++i, subject.name);
      this.insertStmt.bindString(++i, ((Integer)subject.hour).toString());
      this.insertStmt.bindString(++i, subject.day);
      this.insertStmt.bindString(++i, subject.from);
      this.insertStmt.bindString(++i, subject.to);
      this.insertStmt.bindString(++i, subject.teacher);
      this.insertStmt.bindString(++i, subject.room);
      this.insertStmt.bindString(++i, subject.type);
      return this.insertStmt.executeInsert();
   }
   
   public void insertAll(Subject[] subjects)
   {
	   for (Subject subject: subjects)
	   {
		   this.insert(subject);
	   }
   }

   public void deleteAll() {
      this.db.delete(TABLE_NAME, null, null);
   }

   public Subject[] getSubjects()
   {
	   ArrayList<Subject> subjects = new ArrayList<Subject>();

	      Cursor cursor = this.db.query(TABLE_NAME, new String[] {"ident", "name", "hour", "day", "time_from", "time_to", "teacher", "room", "type" }, 
	        null, null, null, null, null);
	      if (cursor.moveToFirst()) {
	         do {
	        	 
	        	 Subject subject = new Subject();
	        	 int i = 0;
	        	 
	        	 subject.ident = cursor.getString(i++);
	        	 subject.name = cursor.getString(i++);
	        	 subject.hour = Integer.parseInt(cursor.getString(i++));
	        	 subject.day = cursor.getString(i++);
	        	 subject.from = cursor.getString(i++);
	        	 subject.to = cursor.getString(i++);
	        	 subject.teacher = cursor.getString(i++);
	        	 subject.room = cursor.getString(i++);
	        	 subject.type = cursor.getString(i++);
	        	 
	        	 
	        	 
	            subjects.add(subject); 
	         } while (cursor.moveToNext());
	      }
	      if (cursor != null && !cursor.isClosed()) {
	         cursor.close();
	      }
	      
	      
	      return subjects.toArray(new Subject[subjects.size()]);
	      
   }
   
   public List<String> selectAll() {
      List<String> list = new ArrayList<String>();
      Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" }, 
        null, null, null, null, "name desc");
      if (cursor.moveToFirst()) {
         do {
            list.add(cursor.getString(0)); 
         } while (cursor.moveToNext());
      }
      if (cursor != null && !cursor.isClosed()) {
         cursor.close();
      }
      return list;
   }
   
   public void close()
   {
	   this.db.close();
   }

   private static class OpenHelper extends SQLiteOpenHelper {

      OpenHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }

      @Override
      public void onCreate(SQLiteDatabase db) {
         db.execSQL("CREATE TABLE " + TABLE_NAME + " (ident VARCHAR(20), name VARCHAR(255), hour INT, day VARCHAR(20), time_from TIME, time_to TIME, teacher VARCHAR(40), room VARCHAR(20), type VARCHAR(20))"
          
          );
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         Log.w("Example", "Upgrading database, this will drop tables and recreate.");
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
         onCreate(db);
      }
   }
}